package com.testdb.Dao;

import com.testdb.Model.Money;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class DaoMoneyHandle {
//第一次svn测试更新
//    public void selectDao() throws Exception {
//        List<Object[]> list = jdbcTools.queryArrayList("select * from money");
//        // ArrayList<Map> maps = jdbcTools.queryMapList("select * from money");
//        System.out.println("---------------零钱通明细----------------");
//        for (int i = 0; i < list.size(); i++) {
//            int len=list.get(i).length-1;
//            for (int i1 = 1; i1 < list.get(i).length-1; i1++) {
//                System.out.print("\t"+list.get(i)[i1]+"\t");
//                //System.out.print(String.format("%-10s%-10s%-10s\n",list.get(i)[i1]));
//
//            }
//            System.out.print("余额:" +list.get(i)[len]);
//            System.out.println();
//            }
//    }
//    public void insertDao() throws Exception {
//        Money money1 = new Money();
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入你的收入类型:");
//        String getorgive = scanner.next();
//        System.out.println("请输入你的收入金额");
//        double money = scanner.nextDouble();
////        money1.setGetorgive(getorgive);
////        money1.setMoney(money);
//        Date date = new Date();
//        SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd HH:mm" );
//        System.out.println("" + smf.format(date));
//        Map<String, Object> balance = jdbcTools.queryMap("select balance from money");
//        Double balance1 = (Double) balance.get("balance");
//        balance1+=money;
//        String jia="+"+money;
////        Map<String, Object> balance1 = jdbcTools.queryMap("select balance from money");
////        System.out.println("balance1 = " + balance1);
////        balance1+=money;
////        money1.setBalance(money1.getBalance());
//        jdbcTools.add("insert into money values(default,?,?,?,?)",getorgive,jia,smf.format(date),balance1);
//
//    }
//    public void insertDao1() throws Exception {
//        Money money1 = new Money();
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入你的消费类型:");
//        String getorgive = scanner.next();
//        System.out.println("请输入你的消费金额");
//        double money = scanner.nextDouble();
////        money1.setGetorgive(getorgive);
////        money1.setMoney(money);
//        Date date = new Date();
//        SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd HH:mm" );
//        System.out.println("" + smf.format(date));
//        Map<String, Object> balance = jdbcTools.queryMap("select balance from money");
//        Double balance1 = (Double) balance.get("balance");
//        if (money>balance1){
//            System.out.println("余额不足");
//            return;
//        }
//        balance1-=money;
//        String jian="-"+money;
////        Map<String, Object> balance1 = jdbcTools.queryMap("select balance from money");
////        System.out.println("balance1 = " + balance1);
////        balance1+=money;
////        money1.setBalance(money1.getBalance());
//        jdbcTools.add("insert into money values(default,?,?,?,?)",getorgive,jian,smf.format(date),balance1);
//    }
private static InputStream in;
    private static SqlSessionFactory factory;
    private static SqlSession sqlSession;
    static MoneyDao moneyDao;
    @Before
    public static void start() throws IOException {
        in = Resources.getResourceAsStream("config/mybatis-config.xml");
        factory = new SqlSessionFactoryBuilder().build(in);
        sqlSession = factory.openSession();
        moneyDao = sqlSession.getMapper(MoneyDao.class);
    }
    @Test
    public void findData() throws IOException {
        DaoMoneyHandle.start();
        List<Money> list = moneyDao.findAll();
        System.out.println("---------------零钱通明细----------------");
        for (int i = 0; i < list.size(); i++) {
                System.out.print("" + list.get(i).getGetorgive()+"\t\t");
                System.out.print("" + list.get(i).getMoney()+"\t\t");
                System.out.print("" + list.get(i).getTime()+"\t\t");
                //System.out.print(String.format("%-10s%-10s%-10s\n",list.get(i)[i1]));
            System.out.print("余额:" +list.get(i).getBalance());
            System.out.println();
            }
        //System.out.print("all = " + all);
    }


    public void findData1() throws IOException {
        DaoMoneyHandle.start();
        Money money1 = new Money();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的收入类型:");
        String getorgive = scanner.next();
        System.out.println("请输入你的收入金额");
        Double money = scanner.nextDouble();
        money1.setGetorgive(getorgive);
        Date date = new Date();
        SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd HH:mm" );
//        System.out.println("" + smf.format(date));
        money1.setTime(smf.format(date));
        Double balance = moneyDao.findone();
        // Double balance1 = (Double) balance.get("balance");
        balance+=money;
        String jia="+"+money;
        money1.setMoney(jia);
        money1.setBalance(balance);
        moneyDao.saveStudent(money1);
        sqlSession.commit();
        System.out.println("添加成功");
    }
    public void findData2() throws IOException {
        DaoMoneyHandle.start();
        Money money1 = new Money();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的消费类型:");
        String getorgive = scanner.next();
        System.out.println("请输入你的支出金额");
        Double money = scanner.nextDouble();
        money1.setGetorgive(getorgive);
        Date date = new Date();
        SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd HH:mm" );
//        System.out.println("" + smf.format(date));
        money1.setTime(smf.format(date));
        Double balance = moneyDao.findone();
        if (money>balance){
            System.out.println("余额不足");
            return;
        }
        balance-=money;
        String jia="-"+money;
        money1.setMoney(jia);
        money1.setBalance(balance);
        moneyDao.saveStudent(money1);
        sqlSession.commit();
        System.out.println("消费成功");
    }
//    //@Test
//    public void delete(int id) throws IOException {
//
//        DaoMoneyHandle.start();
//        moneyDao.deleteStudent(id);
//        sqlSession.commit();
//        System.out.println("删除成功");
//    }

//    public void update(Money student) throws IOException {
//        DaoMoneyHandle.start();
////        Student student = new Student();
////        student.setName("aa");
////        student.setSex("nn");
////        student.setAge(55);
////        student.setMajor("bb");
////        student.setTime("2203-4-4");
////        student.setId(29);
//        moneyDao.updateStudent(student);
//        sqlSession.commit();
//        System.out.println("?????");
//
//    }

//    public void findById(Integer id) throws IOException {
//        TestStudentDao.start();
//        Student byId = studentDao.findById(id);
//        System.out.println("byId = " + byId);
//    }
//    public void findByName(String name) throws IOException {
//        TestStudentDao.start();
//        List<Student> byName = studentDao.findByName(name);
//        System.out.println("byName = " + byName);
//    }
//    public void findTotal() throws IOException {
//        TestStudentDao.start();
//        int total = studentDao.findTotal();
//        System.out.println("total = " + total);
//    }
//    @Test
//    public void login(){
//        User hh = studentDao.login("hh", "123");
//        System.out.println("hh = " + hh);
//    }


//    public static void main(String[] args) throws IOException {
//        DaoMoneyHandle daoMoneyHandle = new DaoMoneyHandle();
        // testStudentDao.findData();
//        Scanner scanner = new Scanner(System.in);
//        Student stu = new Student();
//
//        System.out.println("请输入你要添加的姓名");
//        String aname = scanner.next();
//        System.out.println("1111111");
//        System.out.println("请输入你要添加的性别");
//        String asex = scanner.next();
//        System.out.println("请输入你要添加的年龄");
//        int aage = scanner.nextInt();
//        System.out.println("请输入你要添加的专业");
//        String amajor = scanner.next();
//        stu.setName(aname);stu.setSex(asex);stu.setAge(aage);stu.setMajor(amajor);
//       testStudentDao.findData1(stu);

//        Student stu1 = new Student();
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("????????????????");
//        String sex = scanner.next();
//        System.out.println("?????????????????");
//        int age = scanner.nextInt();
//        System.out.println("?????????????id");
//        int id = scanner.nextInt();
//        stu1.setSex(sex);stu1.setAge(age);stu1.setId(id);
//        testStudentDao.update(stu1);
        //根据id查询
        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入你要查询的id");
        // int id = scanner.nextInt();
//        testStudentDao.findById(id);
//        System.out.println("请输入你要查询的姓名");
//        String name = scanner.next();
//        testStudentDao.findByName(name);
        //testStudentDao.findTotal();
   // }



}
